-- kill_bonus
-- create by zhuangsl
-- 冈布奥主动技：对单体敌人造成伤害；如果该敌人死亡，则获得奖励
-- kill_bonus(slime_skill_id, base, force, bonus[1], bonus[2], bonus[3])
-- slime_skill_id冈布奥主动技id，造成的伤害 = base + force * level, bonus[1~3]是奖励参数（物品或属性）

return {
    apply = function(source, target, skillId, round, para, extra)
        local prop;
        local level;
        extra = extra or {};
        if extra["level"] then
            level = tonumber(extra["level"]);
        else
            prop = PropM.combine(source, "gain_slime_skill", para[1]);
            level = PropM.apply(prop, 0);
        end

        -- 伤害
        local attack = source:queryAttrib("attack");
        local damage = math.modf(attack * (para[2] + para[3] * level) / 1000);

        -- 伤害提升
        prop = PropM.combine(source, "skill_force_add", skillId);
        if prop[3] > 0 then
            damage = PropM.apply(prop, damage);
        end

        -- 斯巴达专属加成
        prop = PropM.combine(source, "battle_cry", 1);
        if prop[3] > 0 then
            -- 触发特技发动事件
            EventMgr.fire(event.SPECIAL_SKILL_TRIGGER, { propId = prop[1] });
        end
        damage = PropM.apply(prop, damage);

        -- 增加命中动作
        SkillM.getSequence(target):hit(source, target, skillId);

        -- 造成伤害
        CombatM.receiveDamage(source, target, damage, skillId, extra);

        -- 如果目标死了，执行奖励
        if target:isDead() then
            local bonus = { para[4], para[5], para[6] };
            BonusM.doBonus(bonus, "skill_kill_bonus");

            -- 抛出通用建筑奖励事件
            EventMgr.fire(event.COMM_BUILDING_BONUS, {
                ["pos"]   = target:getPos(),
                ["bonus"] = { ["bonus"] = bonus },
                ["remainGrid"] = true,
            });
        end

        return { target, };
    end,

    parse = function(desc)
        local arr = string.explode(desc, ",");
        local bonusId = tonumber(arr[4]);
        local bonusKey = iif(bonusId == 1, tonumber(arr[5]), arr[5]);

        return { tonumber(arr[1]), tonumber(arr[2]), tonumber(arr[3]), bonusId, bonusKey, tonumber(arr[6]),};
    end,

    -- 解析技能描述
    parseDesc = function(source, skillId, desc, para)
        local prop = PropM.combine(source, "gain_slime_skill", para[1]);
        local level = prop[3];

        -- 伤害
        local attack = source:queryAttrib("attack");
        local damage = math.modf(attack * (para[2] + para[3] * level) / 1000);

        -- 伤害提升
        prop = PropM.combine(source, "skill_force_add", skillId);
        if prop[3] > 0 then
            damage = PropM.apply(prop, damage);
        end

        -- 斯巴达专属加成
        prop = PropM.combine(source, "battle_cry", 1);
        damage = PropM.apply(prop, damage);

        desc = string.gsub(desc, "{damage}", damage);

        return desc;
    end,
};
